package com.tianji.tools;

import cn.hutool.json.JSONUtil;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseBaseInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CourseTool {
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
public static final String TOOL_CONTEXT_PREFIX = "toolContext:";
    @Tool(description = "根据课程id查询课程信息")
    public CourseInfo queryCourseById(@ToolParam(description = "课程id") Long courseId, ToolContext toolContext) {
        CourseBaseInfoDTO courseBaseInfoDTO = courseClient.baseInfo(courseId, true);
        String requestId = (String) toolContext.getContext().get("requestId");
        log.info("传入的上下文的requestId：{}", requestId);
        CourseInfo courseInfo = CourseInfo.of(courseBaseInfoDTO);
        redisTemplate.opsForHash().put(TOOL_CONTEXT_PREFIX , requestId, JSONUtil.toJsonStr(courseInfo));
        return courseInfo;
    }
}
